package com.ltedu.system.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ltedu.common.contants.Constants;
import com.ltedu.common.validate.Create;
import com.ltedu.common.validate.Update;
import com.ltedu.common.vo.Result;
import com.ltedu.system.domain.SysRole;
import com.ltedu.system.domain.vo.QueryRoleVO;
import com.ltedu.system.domain.vo.SysMenuRoleVO;
import com.ltedu.system.domain.vo.SysRoleVO;
import com.ltedu.system.service.SysRoleService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 角色信息 前端控制器
 * </p>
 *
 * @author Jerry
 * @since 2020-11-27
 */
@RestController
@RequestMapping("/system/role")
@CrossOrigin
public class SysRoleController {

    @Autowired
    private SysRoleService sysRoleService;

    /**
     * 添加角色
     *
     * @param sysRoleVO
     * @return {@link Result}
     */
    @PostMapping("/add")
    public Result add(@Validated(Create.class) @RequestBody SysRoleVO sysRoleVO) {
        return Result.ok(sysRoleService.save(sysRoleVO));
    }

    /**
     * 改变角色状态
     *
     * @param sysRoleVO
     * @return {@link Result}
     */
    @PutMapping("/changeRoleState")
    public Result changeRoleState(@RequestBody SysRoleVO sysRoleVO) {
        return Result.ok(sysRoleService.update(sysRoleVO));
    }

    /**
     * 修改菜单
     *
     * @param sysRoleVO
     * @return {@link Result}
     */
    @PutMapping("/update")
    public Result update(@Validated(Update.class) @RequestBody SysRoleVO sysRoleVO) {
        return Result.ok(sysRoleService.update(sysRoleVO));
    }

    /**
     * 根据id删除角色信息
     *
     * @param id id
     * @return {@link Result}
     */
    @DeleteMapping("/remove/{id}")
    public Result remove(@PathVariable Long id) {
        return Result.ok(sysRoleService.removeById(id));
    }

    /**
     * 批量删除
     *
     * @param idList id列表
     * @return {@link Result}
     */
    @DeleteMapping("/removeBatchs")
    public Result removeBatchs(@RequestBody List<Long> idList) {
        return Result.ok(sysRoleService.removeByIds(idList));
    }

    /**
     * 根据id获取角色信息
     *
     * @param id id
     * @return {@link Result}
     */
    @GetMapping("/get/{id}")
    public Result get(@PathVariable Long id) {
        return Result.ok(sysRoleService.getById(id));
    }

    /**
     * 分页查询角色列表
     *
     * @param page        页面
     * @param limit       限制
     * @param queryRoleVO 查询角色签证官
     * @return {@link Result}
     */
    @PostMapping("/list/{page}/{limit}")
    public Result list(@PathVariable("page") Integer page,
                       @PathVariable("limit") Integer limit,
                       @RequestBody QueryRoleVO queryRoleVO) {
        Page<SysRole> pageParam = new Page<>(page, limit);
        LambdaQueryWrapper<SysRole> queryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotEmpty(queryRoleVO.getRoleName())) {
            queryWrapper.like(SysRole::getRoleName, queryRoleVO.getRoleName());
        }
        if (StringUtils.isNotEmpty(queryRoleVO.getRoleKey())) {
            queryWrapper.eq(SysRole::getRoleKey, queryRoleVO.getRoleKey());
        }
        if (queryRoleVO.getState() != null) {
            queryWrapper.eq(SysRole::getState, queryRoleVO.getState());
        }
        if (queryRoleVO.getBegin() != null) {
            queryWrapper.gt(SysRole::getGmtCreate, queryRoleVO.getBegin());
        }
        if (queryRoleVO.getEnd() != null) {
            queryWrapper.lt(SysRole::getGmtCreate, queryRoleVO.getEnd());
        }
        queryWrapper.orderByDesc(SysRole::getGmtCreate);
        Page<SysRole> records = sysRoleService.page(pageParam, queryWrapper);
        return Result.ok(records);
    }

    /**
     * 获取所有角色列表
     *
     * @return {@link Result}
     */
    @GetMapping("/all")
    public Result all(){
        return Result.ok(sysRoleService.list(new LambdaQueryWrapper<SysRole>().
                eq(SysRole::getState, Constants.NORMAL)));
    }

    /**
     * 角色授权
     *
     * @param sysMenuRoleVo 系统菜单的角色签证官
     * @return {@link Result}
     */
    @PostMapping("/save")
    public Result save(@RequestBody SysMenuRoleVO sysMenuRoleVo) {
        return Result.ok(sysRoleService.saveMenuRole(sysMenuRoleVo));
    }

}

